package com.blog.interceptor;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

public class ScriptInterceptor extends BaseInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(request.getMethod().equals("POST")) {
            @SuppressWarnings({"rawtypes" })
            Enumeration params = request.getParameterNames();
            while(params.hasMoreElements()) {
                Object paramName = params.nextElement();
                logger.info(paramName.toString());
                if(paramName instanceof String) {
                    String paramValue = request.getParameter(paramName.toString());
                    if(paramValue.contains("<script>"))
                        return false;
                }
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}
